#include "../header/picasa_ui.h"

#include <iostream>
using namespace std;

PicasaUI::PicasaUI( const QStringList& argsList )
{
	args = argsList;
	if( !AreNParameters(args, 1) )
	{
		cout << "[Picasa] Need more parameters" << endl;
		cout << "[Picasa] Functions are:" << endl;
		cout << "[Picasa] GetExistingAlbums Username Password" << endl;
		cout << "[Picasa] PostImages LocalDirectory AlbumName UsernamePassword" << endl;
		cout << endl;
		cout << "[Picasa] Thank you for using Picasa plugin, have fun! :)" << endl;
		
		return;
	}
	
	connect(&pikaska, SIGNAL( siAuth( const StringMap& ) ),
             this, SLOT( slOnAuth( const StringMap& ) ) );

	connect(&pikaska, SIGNAL( siThrowError(  int, const QString& ) ),
             this, SLOT( slOnError(  int, const QString& ) ) );
	
	connect(&pikaska, SIGNAL( siGetExistingAlbums( const StringVector& ) ),
             this, SLOT( slGetExistingAlbums( const StringVector& ) ) );
	

	connect(&pikaska, SIGNAL( siPostImage() ),
             this, SLOT( slPostNextImage() ) );
	
	CurrentAction = args.takeAt(0);
	if( CurrentAction == "GetExistingAlbums" )
	{
		if( !AreNParameters( args, 2 ) )
		{
			cout << "[Picasa] Parameters for using GetExistingAlbums function:" << endl;
			cout << "GetExistingAlbums Username Password" << endl;
			return;
		}
		
		UserName = args.takeFirst();
		Password = args.takeFirst();
		
		GetExistingAlbums();
		return;
	}
	else if( CurrentAction == "PostImages" )
	{
		if( !AreNParameters( args, 4 ) )
		{
			qDebug() << "This is man for PostImages funct";
			return;
		}
		
		Directory = args.takeFirst();
		AlbumTitle = args.takeFirst();
		UserName = args.takeFirst();
		Password = args.takeFirst();
		
		PostImages( Directory );
	}
}

PicasaUI::PicasaUI()
{
	return;
}

bool PicasaUI::AreNParameters( const QStringList& args, int n )
{
	if(args.size() >= n)
		return true;

	return false;
}

int PicasaUI::GetExistingAlbums()
{
	//disconnect(&pikaska, SIGNAL( siGetExistingAlbums( const StringVector& ) ),
  //           this, SLOT( slGetExistingAlbums( const StringVector& ) ) );

	

	cout << "[Picasa] Getting existing albums" << endl;
	
	pikaska.GetExistingAlbums();

	return 0;
}

void PicasaUI::Auth( const QString& UserName, const QString& Password )
{
	//disconnect(&pikaska, SIGNAL( siAuth( const StringMap& ) ),
  //           this, SLOT( slOnAuth( const StringMap& ) ) );

	

	cout << "[Picasa] Authing..." << endl;
	pikaska.Auth( UserName, Password);
}
void PicasaUI::slOnAuth( const StringMap& map )
{
	cout << "[Picasa] Authed successfuly!" << endl;
	//disconnect(&pikaska, SIGNAL(siAuth( const StringMap& )), 0, 0);
	if( CurrentAction == "GetExistingAlbums" )
	{
		GetExistingAlbums();
	}
	else if( CurrentAction == "PostImages" )
	{
		PostNextImage();
	}
}

void PicasaUI::slGetExistingAlbums( const StringVector& vector )
{
	//disconnect(&pikaska, SIGNAL( siGetExistingAlbums( const StringVector& ) ),
  //           this, SLOT( slGetExistingAlbums( const StringVector& ) ) );

	if( !vector.size() )
	{
		cout << "There are no albums, go and create one!" << endl;
		return;
	}
	//qDebug() << "Titles Vector" << vector;
	cout << "Your albums:" << endl;
	for( int i = 0; i < vector.size(); i++)
	{
		cout << vector.at(i).toStdString() << endl;
	}	
}

void PicasaUI::PostImages( const QString& Directory )
{
	CurrentAction = "PostImages";
	QDir dir( Directory, "", QDir::Name | QDir::IgnoreCase, QDir::Files);
	
	fileList = dir.entryInfoList();
	PostNextImage();
}

void PicasaUI::PostNextImage()
{
	//qDebug() << "In NextImage";
	if( !fileList.size() )
	{
		qDebug() << "All images uploaded!";
		return;
	}
	qDebug() << "In NextImage after return;";
	QString CurrentFilePath( fileList.first().filePath() );
	
	qDebug() << "Tryin to upload: " << CurrentFilePath;
	
	pikaska.PostImageRequest( AlbumTitle, CurrentFilePath );
}

void PicasaUI::slPostNextImage()
{
	qDebug() << "Finished: " << fileList.takeFirst().filePath();
	PostNextImage();
}
void PicasaUI::slOnError(  int ErrorId, const QString& ErrorString )
{
	//disconnect(&pikaska, SIGNAL( siThrowError(  int, const QString& ) ),
  //           this, SLOT( slOnError(  int, const QString& ) ) );

	//connect(&pikaska, SIGNAL( siThrowError(  int, const QString& ) ),
  //           this, SLOT( slOnError(  int, const QString& ) ) );

	if( ErrorString == "UnsuportedImageType")
	{
		qDebug() << "UnsuportedImageType: " << fileList.takeFirst().filePath();
		PostNextImage();
	}
	else if( ErrorString == "CannotOpenReadOnlyImage")
	{
		qDebug() << "CannotOpenReadOnlyImage: " << fileList.takeFirst().filePath();
		PostNextImage();
	}
	else if( ErrorString == "AuthNeeded" )
	{
		cout << "[Picasa] Need to auth, in order to use this function" << endl;
		Auth( UserName, Password );
	}
	else
	{	
		qDebug() << "Error, with error id: " << ErrorId << "Error string: " << ErrorString;
	}
}

//END

